package com.xm.xmapp;

import java.lang.reflect.Field;
import java.util.ArrayList;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.xm.xmapp.adapter.MyPagerAdapters;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.CustomTextView;
import com.xm.xmapp.fragment.MessageActivityFragment;
import com.xm.xmapp.fragment.MessageMyAssetFragment;
import com.xm.xmapp.fragment.MessageSystemFragment;
import com.xm.xmapp.response.bean.ResponseAllMessageId;
import com.xm.xmapp.tools.AndroidTool;
import com.xm.xmapp.tools.Helper_SharedPreferences;
import com.xm.xmapp.tools.Tools;



public class MessageActivity extends BaseActivity {
	private ViewPager vp;
	private TabLayout tab;
	private MyPagerAdapters adapter;
	ChangViewListener changViewListener;
	private CustomTextView[] views = new CustomTextView[3];

	/**
	 * 1=可用 2=不可用 3=已使用
	 */
	private String[] title = {"系统消息", "活动消息", "我的消息"};

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_yhq);
	}

	public interface ChangViewListener {
		void setView(int flag);
	}

	@Override
	public void initView() {
		vp = (ViewPager) findViewById(R.id.vp);
		tab = (TabLayout) findViewById(R.id.tab);

	}

	@Override
	public void initData() {

		changViewListener = new ChangViewListener() {

			@Override
			public void setView(int flag) {
				setMessage();
			}
		};

		setMyTitle("消息");
		adapter = new MyPagerAdapters(title, getSupportFragmentManager(), new ArrayList<Fragment>());
		vp.setBackgroundResource(R.color.white);
		MessageSystemFragment systemMessageFrament = new MessageSystemFragment();
		adapter.fragmentsList.add(systemMessageFrament);

		MessageActivityFragment activityFrament = new MessageActivityFragment(changViewListener);
		adapter.fragmentsList.add(activityFrament);

		MessageMyAssetFragment myAssetFrament = new MessageMyAssetFragment(changViewListener);
		adapter.fragmentsList.add(myAssetFrament);

		// 设置Viewpage属性
		vp.setAdapter(adapter);
		vp.setOffscreenPageLimit(3);
		tab.setupWithViewPager(vp);
		for (int i = 0; i < tab.getTabCount(); i++) {
			    android.view.View view = LayoutInflater.from(this).inflate(R.layout.layout,null);
			    views[i] = (CustomTextView) view.findViewById(R.id.title);
			    views[i].setText(title[i]);
			    views[i].setGravity(Gravity.CENTER);
			    views[i].setVisibility(CustomTextView.InnerStatus.GONE);
			    tab.getTabAt(i).setCustomView(views[i]);
		}
		tab.post(new Runnable() {
			@Override
			public void run() {
				int marig = Tools.dp2px(5);
				AndroidTool.setIndicator(tab,marig,marig);
			}
		});

		setMessage();
	}



	private void setMessage() {

		ResponseAllMessageId bean2 = (ResponseAllMessageId) Helper_SharedPreferences
				.get_obj_sp(CodeConstant.messageClient);
		ResponseAllMessageId bean1 = (ResponseAllMessageId) Helper_SharedPreferences
				.get_obj_sp(CodeConstant.messageServer);
		if (bean2 != null) {
			if (bean2.activity_max_id < bean1.activity_max_id) {
				views[1].setVisibility(CustomTextView.InnerStatus.VISIBLE);
			} else {
				views[1].setVisibility(CustomTextView.InnerStatus.GONE);
			}
			if (bean2.user_message_max_id < bean1.user_message_max_id) {
				views[2].setVisibility(CustomTextView.InnerStatus.VISIBLE);
			} else {
				views[2].setVisibility(CustomTextView.InnerStatus.GONE);
			}
		} else {
			if (bean1 == null || bean1.activity_max_id > 0) {
				views[1].setVisibility(CustomTextView.InnerStatus.VISIBLE);
			} else {
				views[1].setVisibility(CustomTextView.InnerStatus.GONE);
			}
			if (bean1 == null || bean1.user_message_max_id > 0) {
				views[2].setVisibility(CustomTextView.InnerStatus.VISIBLE);
			} else {
				views[2].setVisibility(CustomTextView.InnerStatus.GONE);
			}
		}

	}

	@Override
	public void initEvent() {
	}

}
